package com.morningstar.qs.api.basic.util;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;

/**
 * @deprecated
 * @author dchen2
 *
 */
public class HttpUtil {
	public static final int DEFAULT_HTTP_TIMEOUT_MILLIS = 1000 * 30;
	
	/**
	 * Obtain string data data with HTTP or HTTPS protocol
	 *
	 * @deprecated
	 * 
	 * @param URL
	 * @return
	 * @throws HandlerException
	 * @throws MalformedURLException
	 */
	public static String getDataWithHttpProtocol(String URL) throws MalformedURLException, IOException {
		int connectTimeout = DEFAULT_HTTP_TIMEOUT_MILLIS;
		int readTimeout = DEFAULT_HTTP_TIMEOUT_MILLIS;
		return getDataWithHttpProtocol(URL,connectTimeout,readTimeout);
	}
	
	/**
	 * Obtain string data data with HTTP or HTTPS protocol
	 * @deprecated
	 * @param URL
	 * @return
	 * @throws HandlerException
	 * @throws MalformedURLException
	 */
	public static String getDataWithHttpProtocol(String URL,int connectTimeOut,int readTimeOut) throws MalformedURLException, IOException {
		HttpURLConnection con = null;
		HttpURLConnection.setFollowRedirects(true);
		StringBuffer sb = new StringBuffer();
		URL urlmy = new URL(URL);
		con = (HttpURLConnection) urlmy.openConnection();
		con.setConnectTimeout(connectTimeOut);
		con.setReadTimeout(readTimeOut);
		con.setInstanceFollowRedirects(false);

		con.connect();
		BufferedReader br = new BufferedReader(new InputStreamReader(con.getInputStream(), "UTF-8"));

		String s = "";

		while ((s = br.readLine()) != null) {
			sb.append(s);
		}

		if (con != null) {
			con.disconnect();
		}
		if (sb.length() > 0)
			return sb.toString();
		return null;
	}
}
